Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n) time and without using the division operation.
Example 1:
Input: nums = [1,2,3,4] Output: [24,12,8,6]
Example 2:
Input: nums = [-1,1,0,-3,3] Output: [0,0,9,0,0]
Constraints:
2 <= nums.length <= 105-30 <= nums[i] <= 30- The product of any prefix or suffix of
numsis guaranteed to fit in a 32-bit integer.
Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)
Average Rating: 4.65 (285 votes)
Solution
From the looks of it, this seems like a simple enough problem to solve in linear time and space. We can simply take the product of all the elements in the given array and then, for each of the elements x of the array, we can simply find product of array except self value by dividing the product by x.
Doing this for each of the elements would solve the problem. However, there's a note in the problem which says that we are not allowed to use division operation. That makes solving this problem a bit harder.
Approach 1: Left and Right product lists
It's much easier to build an intuition for solving this problem without division once you visualize how the different products except self look like for each of the elements. So, let's take a look at an example array and the different products.

Looking at the figure about we can figure another way of computing those different product values.
Instead of dividing the product of all the numbers in the array by the number at a given index to get the corresponding product, we can make use of the product of all the numbers to the left and all the numbers to the right of the index. Multiplying these two individual products would give us the desired result as well.
For every given index, i, we will make use of the product of all the numbers to the left of it and multiply it by the product of all the numbers to the right. This will give us the product of all the numbers except the one at the given index i. Let's look at a formal algorithm describing this idea more concretely.
Algorithm
- Initialize two empty arrays,
LandRwhere for a given indexi,L[i]would contain the product of all the numbers to the left ofiandR[i]would contain the product of all the numbers to the right ofi. - We would need two different loops to fill in values for the two arrays. For the array
L, L[0] would be1since there are no elements to the left of the first element. For the rest of the elements, we simply use L[i]=L[i−1]∗nums[i−1]. Remember thatL[i]represents product of all the elements to the left of element at index i. - For the other array, we do the same thing but in reverse i.e. we start with the initial value of
1in R[length−1] where length is the number of elements in the array, and keep updatingR[i]in reverse. Essentially, R[i]=R[i+1]∗nums[i+1]. Remember thatR[i]represents product of all the elements to the right of element at index i. - Once we have the two arrays set up properly, we simply iterate over the input array one element at a time, and for each element at index
i, we find theproduct except selfas L[i]∗R[i].
Let's go over a simple run of the algorithm that clearly depicts the construction of the two intermediate arrays and finally the answer array.
For the given array [4,5,1,8,2], the L and R arrays would finally be:

Complexity analysis
- Time complexity : O(N) where N represents the number of elements in the input array. We use one iteration to construct the array L, one to construct the array R and one last to construct the answer array using L and R.
- Space complexity : O(N) used up by the two intermediate arrays that we constructed to keep track of product of elements to the left and right.
Approach 2: O(1) space approach
Although the above solution is good enough to solve the problem since we are not using division anymore, there's a follow-up component as well which asks us to solve this using constant space. Understandably so, the output array does not count towards the space complexity. This approach is essentially an extension of the approach above. Basically, we will be using the output array as one of L or R and we will be constructing the other one on the fly. Let's look at the algorithm based on this idea.
Algorithm
- Initialize the empty
answerarray where for a given indexi,answer[i]would contain the product of all the numbers to the left ofi. - We construct the
answerarray the same way we constructed theLarray in the previous approach. These two algorithms are exactly the same except that we are trying to save up on space. - The only change in this approach is that we don't explicitly build the
Rarray from before. Instead, we simply use a variable to keep track of the running product of elements to the right and we keep updating theanswerarray by doing answer[i]=answer[i]∗R. For a given indexi,answer[i]contains the product of all the elements to the left andRwould contain product of all the elements to the right. We then updateRas R=R∗nums[i]
Complexity analysis
- Time complexity : O(N) where N represents the number of elements in the input array. We use one iteration to construct the array L, one to update the array answer.
- Space complexity : O(1) since don't use any additional array for our computations. The problem statement mentions that using the answer array doesn't add to the space complexity.
October 26, 2019 12:03 PM
How in the world can I ever think of this in an interview
We can utilize properties of log, if input array is [a, b, c, d] then
precalculated_val = log(a* b* c* d) = log(a) + log(b) + log(c) + log(d)
So for output array it would be { antilog [precalculated_val - log(arr[i])] }
June 17, 2019 5:53 AM
Don't you think that the second approach is NOT in O(1) space complexity? We are still using ONE intermediate array (answer) to make the L array. So it is O(N), right?
So what is the purpose of this kind of problem? I give up...
Last Edit: June 14, 2019 1:07 AM
There's more to the simple solution using division than this article suggests. I challenge people to try to implement that solution. I predict that most will not get it right on their first try.
(If you'd like to try, refrain from opening / reading the replies to this comment.)
It's slightly quicker if you don't use reversed (faster than 99% of submissions)
class Solution(object):
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
answer = [1]*len(nums)
answer[0] = 1
for i in range(1, len(nums)):
answer[i] = answer[i-1] * nums[i-1]
rightProduct = 1
for i in range(len(nums)-1, -1, -1):
answer[i] = answer[i] * rightProduct
rightProduct *= nums[i]
return answerI did it this way:
-
If there is a single zero in the list, every other elements will be zero expect for that
-
If there are more than one zero in the list, every element will be zero
-
Else we calculate prod = product of every element, answer is product divided by individual items
def productExceptSelf(self, nums): """ :type nums: List[int] :rtype: List[int] """ zero_loc = -1 prod = 1 for i, num in enumerate(nums): if num == 0: if zero_loc == -1: zero_loc = i else: zero_loc = "multiple" else: prod *= num if zero_loc == "multiple": return [0 for x in nums] elif zero_loc != -1: return [0 if i != zero_loc else prod for i, num in enumerate(nums)] else: return [prod/x for x in nums]
The description should contain a note that there are not 0 in the array, otherwise, the solution above won't work.
August 22, 2019 9:20 AM
Is the space complexity of the second approach O(1)? I think it's O(n)
xxxxxxxxxxclass Solution {public: // O(n) time and O(n) space vector<int> productExceptSelf(vector<int>& nums) { int n = nums.size(); vector<int> prefix(n, 1), suffix(n, 1); for(int i=1;i<n;i++) { prefix[i] = prefix[i-1] * nums[i-1]; } for(int i=n-2;i>=0;i--) { suffix[i] = suffix[i+1] * nums[i+1]; } vector<int>res(n); for(int i=0;i<n;i++) { res[i] = prefix[i] * suffix[i]; } return res; }};